package com.example.speeddating.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.speeddating.entity.Message;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

/**
 *
 * @author tina
 * @since 2023-06-28 11:14:33
 */
@Mapper
public interface MessageMapper extends BaseMapper<Message> {

    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "from",property = "fromUser",
                    one = @One(select = "com.example.speeddating.mapper.UserMapper.selectUser")),
            @Result(column = "to", property = "toUser",
                    one = @One(select = "com.example.speeddating.mapper.UserMapper.selectUser")),
    })
    @Select("SELECT m.* FROM `message` m JOIN ( SELECT CASE \n" +
            "WHEN `from` = #{userId} THEN `to` ELSE `from`\n" +
            "END AS user_id,MAX(create_time) AS max_create_time\n" +
            "FROM `message` WHERE `from` = #{userId} OR `to` = #{userId} GROUP BY CASE \n" +
            "WHEN `from` = #{userId} THEN `to` ELSE `from`\n" +
            "END)t ON ((m.`from` = #{userId} AND m.`to` = t.user_id) OR (m.`from` = t.user_id AND m.`to` = #{userId})) AND m.create_time = t.max_create_time\n" +
            "WHERE NOT EXISTS (\n" +
            "  SELECT 1\n" +
            "  FROM blacklist\n" +
            "   WHERE (black_user_id = t.user_id) AND deleted = 0\n" +
            ")\n" +
            " ORDER BY m.create_time DESC")
    public Page<Message> selectMessageList(Page<Message> messagePage,long userId);


    @Select("SELECT count(*) FROM message m WHERE m.`from` = #{from} and m.`to` = #{to} AND m.is_read = 0")
    public Integer selectCountUnread(@Param("from") long from, @Param("to") long to);


    /**
     * 查询聊天记录
     * @param messagePage
     * @param from
     * @param to
     * @return
     */
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "from", property = "from"),
            @Result(column = "to", property = "to"),
            @Result(column = "from",property = "fromUser",
                    one = @One(select = "com.example.speeddating.mapper.UserMapper.selectUser")),
            @Result(column = "to", property = "toUser",
                    one = @One(select = "com.example.speeddating.mapper.UserMapper.selectUser")),
    })
    @Select("SELECT m.* FROM message as `m` \n" +
            "WHERE m.`from` = #{from} and m.`to` = #{to}\n" +
            "UNION\n" +
            "SELECT m.* FROM\n" +
            "message as `m`\n" +
            "WHERE m.`from` = #{to} and m.`to` = #{from}  ORDER BY create_time desc")
    Page<Message> selectMessageRecords(Page<Message> messagePage,Long from, Long to);

    @Update(value = "UPDATE message set is_read = 1 WHERE `from` = #{from} AND `to` = #{to} and is_read = 0")
    void updateUnreadMessage(Long from, Long to); //to就是当前的userId


    @Insert(value = "INSERT INTO message VALUES(0,#{from},#{content},#{to},#{type},#{isRead},NOW(),#{url},#{longitude},#{latitude})")
    void insertMessage(Message message);
}
